﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class UnSetWork
    {

        private static UnSetWork _unSetWorkInstance;
        public static UnSetWork UnSetWorkInstance
        {
            get { return _unSetWorkInstance ?? (_unSetWorkInstance = new UnSetWork()); }
        }

        /// <summary>
        /// 获取不固定工作量信息 
        /// </summary>
        /// <returns></returns>
        public List<t_UnSetWork> FindAll()
        {
            using (DBEntities db = new DBEntities())
            {
                IEnumerable<t_UnSetWork>  list =
                    from unSetWork in db.t_UnSetWork
                   
                    select unSetWork;
               
                List<t_UnSetWork> newList = (List<t_UnSetWork>)list.ToList();
                return newList;
            }
        }

        /// <summary>
        /// 获取职务信息  
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public List<t_UnSetWork> FindAll(string schoolMark)
        {
            using (DBEntities db = new DBEntities())
            {
                IEnumerable<t_UnSetWork> list =
                    from work in db.t_UnSetWork
                    where work.SchoolMark == schoolMark
                    select work;
                            //{
                            //    RID = work.RID,
                            //    WorkName = work.WorkName,
                            //    ShcoolMark = work.SchoolMark,
                            //    Acc = work.ACC.HasValue ? work.ACC : 0,
                            //    ConvertNum = work.ConvertNum
                            //};

                List<t_UnSetWork> newList = (List<t_UnSetWork>)list.ToList();
                return newList;
            }
        }

        public int FindCount(t_UnSetWork entity)
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from b in db.t_UnSetWork
                             where b.WorkName == entity.WorkName
                             select b).Count();
                return count;
            }
        }


        /// <summary>
        /// 添加新职称
        /// </summary>
        /// <param name="entity">职称实体</param>
        /// <returns>是否成功</returns>
        public bool Add(t_UnSetWork entity, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int wrokName = (from b in db.t_UnSetWork
                                 where b.WorkName == entity.WorkName
                                 select b).Count();
                if (wrokName > 0)
                {
                    msg = "已经存在名称为【" + entity.WorkName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    db.AddTot_UnSetWork(entity);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 更新acc  acc为1 表示参与  acc为0 不参与
        /// </summary>
        /// <param name="workId"></param>
        /// <param name="acc"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool UpdateAcc(int workId,int acc,string schoolMark)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var workList = from b in db.t_UnSetWork
                               where b.RID == workId && b.SchoolMark == schoolMark
                               select b;

                if(workList.Count() > 0)
                {
                    foreach (var tUnSetWork in workList)
                    {
                        tUnSetWork.ACC = acc;
                    }    
                } 
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    result = false;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改车辆
        /// </summary>
        /// <param name="entity">车辆实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Update(t_UnSetWork entity, ref string msg)
        {
            bool result = true;

            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int workName = (from b in db.t_Post
                                 where b.PostName == entity.WorkName && b.RID != entity.RID
                                 select b).Count();
                if (workName > 0)
                {
                    msg = "已经存在名称为【" + entity.WorkName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    var query =
                        from ord in db.t_UnSetWork
                        where ord.RID == entity.RID
                        select ord;

                    foreach (t_UnSetWork item in query)
                    {
                        item.WorkName = entity.WorkName;
                        item.ConvertNum = entity.ConvertNum;
                    }

                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    } 
                }
                return result;
            }
        }


        /// <summary>
        /// 修改不固定工作量信息
        /// </summary>
        /// <param name="workName"></param>
        /// <param name="converNum"></param>
        /// <returns></returns>
        public int UpdateWorkInfo(string workName,int converNum)
        {
            using (DBEntities db = new DBEntities())
            {

                return 0;
            }
        }


        /// <summary>
        /// 修改车辆
        /// </summary>
        /// <param name="entity">车辆实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Delete(t_UnSetWork entity, ref string msg)
        {
            bool result = true;

            using (DBEntities db = new DBEntities())
            {
                var list = from work
                           in db.t_UnSetWork
                           where work.RID == entity.RID
                           select work;
                foreach (t_UnSetWork tempWork in list.ToList<t_UnSetWork>())
                {
                    try
                    {
                        db.DeleteObject(tempWork);
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                return result;
            }
        }


        public int Create(t_UnSetWork info)
        {
            return 0;
        }
      
    }
}
